//文章模块
define('article', ['config', 'cookie', 'jquery', 'handlebars', 'showBigImg'], function(config, cookie, $, Handlebars, showBigImg){
	//定义hanlebars helper
	function helper(){
		//判断头像是否为空
		Handlebars.registerHelper('if_empty', function(value, options){
			if(value == ''){
				return options.fn(this);
			}
			else{
				return options.inverse(this);
			}
		});
	}

	//时间戳处理
	function process_timestamp(data){
		var exp = new Date();
		exp.setTime(data);
		//把时间分开装入数组，对只有一位的值进行前补0处理
		var timeArray = [exp.getFullYear(), (exp.getMonth()+1), 
			exp.getDate(), exp.getHours(), exp.getMinutes(), exp.getSeconds()];
		for (var j = 0; j < 6; j++) {
			if (timeArray[j].toString().length == 1){
				timeArray[j] = '0' + timeArray[j];
			}
		}
		var publishTime = timeArray[0] + '.' + timeArray[1] + '.' + timeArray[2]
				+ '  ' + timeArray[3] + ':' + timeArray[4] + ':' + timeArray[5];

		return publishTime;
	}
	

	//返回函数列表
	return {
		getArticles: function(sort, skip, total, clear, city, callback){
			var request = $.ajax({
				url: config['api']['getArticles'][0],
				type: config['api']['getArticles'][1],
				data: {
					city: city,
					sort: sort,
					skip: skip,
					total: total,
				},
				dataType: 'json',
				async: true,
				error: function(err){
					console.log('get articles failed!');
				},
				success: function(data){
					//处理数据(添加publishTime)
					for (var i = 0; i < data['articles'].length; i++) {
						var publishTime = process_timestamp(data['articles'][i]['createTime']);
						data['articles'][i]['publishTime'] = publishTime;
					}

					console.log(data);
					//注册helper
					helper();
					//模板生成中心问题
					var source = $("#articles-template").html();
					var template = Handlebars.compile(source);

					//根据clear参数判断是否清楚之前文章
					if (clear){
						var sorting = '<div class="sorting-div">' + $('.sorting-div').html() + '</div>';
						$('.center-problem').html('');
						$('.center-problem').html(sorting + template(data));
					}
					else{
						$('.center-problem').html($('.center-problem').html() + template(data));
					}

					//点开大图模块
					$('.articles .paintings img').on('click', function(){
						var src = $(this).attr('src');
						showBigImg(src);
					});


					//调用回调
					if (callback){
						callback(data['articles'].length);
					}
				}
			});
		},
		getArticleById: function(articleId){
			//获取中心文章内容
			var problem_request = $.ajax({
				url: config['api']['getArticleById'][0].replace('<article_id>', articleId),
				type: config['api']['getArticleById'][1],
				dataType: 'json',
				async: true,
				error: function(err){
					console.log('get article failed!');
				},
				success: function(data){
					//处理数据(添加publishTime)
					var publishTime = process_timestamp(data['article']['createTime']);
					data['article']['publishTime'] = publishTime;

					console.log(data);
					//注册helper
					helper();
					//模板生成中心问题
					var source = $("#articles-template").html();
					var template = Handlebars.compile(source);
					$('.article-detials').html($('.article-detials').html() + template(data['article']));
					//点开大图模块
					$('.articles .paintings img').on('click', function(){
						var src = $(this).attr('src');
						showBigImg(src);
					});
				}
			});
		}
	};
});
